package org.example.zhihuan.services;

import org.example.zhihuan.services.projectException.*;
import org.example.zhihuan.util.Db;

import java.sql.ResultSet;
import java.util.Date;

public class Administrators {
    private static int ADMIN_ONLINE = 0;//管理员在线
    private static int ADMIN_UNLINE = 1;//管理员不在线

    private int adminId;               //管理员id
    private String adminName;             //管理员name
    private String adminPsw;              //管理员密码
    private int adminState;                 //管理员状态

    private boolean isSaved = false;       //初始值

    public int getAdminId() {
        return adminId;
    }

    public String getAdminName() {
        return adminName;
    }

    public String getAdminPsw() {
        return adminPsw;
    }

    public int getAdminState() {
        return adminState;
    }

    public boolean isSaved() {
        return isSaved;
    }

    /**
     * 管理员的创建
     */
    public static Administrators createAdmin(String adminName, String adminPsw) {
        Administrators admin = new Administrators();
        admin.adminName = adminName;
        admin.adminPsw = adminPsw;

        admin.save();

        return admin;
    }

    /**
     * 持久化
     */
    private void save() {
        StringBuilder sb = new StringBuilder();
        if (isSaved) {
            sb.append("update admin set ")
                    .append(" admin_name = '").append(adminName).append("',")
                    .append(" admin_psw = '").append(adminPsw).append("',")
                    .append(" admin_state = ").append(adminState).append(" ")
                    .append("where admin_id = ").append(adminId);
        } else {
            sb.append("insert into admin (admin_name,admin_psw,admin_state) values " + "('")
                    .append(adminName).append("','").append(adminPsw).append("',").append(adminState).append(")");
            isSaved = true;
        }
        Db.execute(sb.toString());
    }

    /**
     * 判断管理员是否存在
     *
     * @param adminId
     * @return
     */
    public static boolean adminIdExist(int adminId) {
        return Db.read("select 1 from admin where admin_id = " + adminId, ResultSet::next);
    }

    /**
     * 获取管理员id
     *
     * @param adminId
     * @return
     */
    public static Administrators getAdmin(int adminId) {
        if (!Administrators.adminIdExist(adminId)) {
            throw new AdminException("管理员不存在");
        }
        Administrators admin = new Administrators();
        Db.read("select * from admin where admin_id = " + adminId, set -> {
            if (set.next()) {
                admin.adminId = set.getInt("admin_id");
                admin.adminName = set.getString("admin_name");
                admin.adminPsw = set.getString("admin_psw");
                admin.adminState = set.getInt("admin_state");
                admin.isSaved = true;
            }
            return null;
        });
        return admin;
    }

    /**
     * 管理员审核商品
     */
    public void adminCheck(boolean check, int goodId) {
        if (this.adminState == ADMIN_UNLINE)
            throw new AdminException("管理员不在线");
        if (Goods.goodsIdExist(goodId)) {
            Goods.getGoods(goodId).checkGoods(check);
            Check.createCheck(goodId, this.adminId, new Date());
        } else {
            throw new GoodsExistException("商品不存在");
        }
    }

    /**
     * 管理员审核评论
     *
     * @param checkAss
     * @param assId
     */
    public void adminCheckAss(boolean checkAss, int assId) {
        if (this.adminState == ADMIN_UNLINE)
            throw new AdminException("管理员不在线");

        if (Assessment.assIdExist(assId)) {
            Assessment ass = Assessment.getAssessment(assId);
            ass.assCheck(checkAss,this.adminId);
        } else
            throw new AssIdExistException("评论不存在");
    }

    /**
     * 管理员审核投诉
     *
     * @param checkComp
     * @param compId
     */
    public void adminCheckComp(boolean checkComp, int compId) {
        if (this.adminState == ADMIN_UNLINE)
            throw new AdminException("管理员不在线");
        if (Complaint.compIdExist(compId)) {
            Complaint.getComplaint(compId).checkComp(checkComp,adminId);
            Check.createCheck(compId, this.adminId, new Date());
        } else throw new CompedException("投诉不存在");
    }

    /**
     * 管理员封禁账号
     */
    public void adminBan(boolean ban, int userId) {
        if (this.adminState == ADMIN_UNLINE) {
            throw new AdminException("管理员不在线");
        }
        if (!User.userIsSavedById(userId)) {
            throw new UserIsSavedException("用户不存在");
        }
        if (User.userIsBan(userId)) {
            throw new UserStateException("用户已封禁");
        }
        User user = User.getUser(userId);
        if (user.getUserState() == User.NORMAL) {
            user.userBan(ban);
        }
    }

    /**
     * 管理员删除评论
     */
    public void adminAssDelete(int assId) {
        if (this.adminState == ADMIN_UNLINE)
            throw new AdminException("管理员不在线");
        if (Assessment.assIdExist(assId)) {
            Assessment.getAssessment(assId).assAdminDelete(this.adminId);
        } else throw new AssIdExistException("评论不存在");
    }

    /**
     * 管理员下架商品
     *
     * @param goodsId
     * @return
     */
    public boolean adminGoodsDown(int goodsId) {
        if (this.adminState == ADMIN_UNLINE)
            throw new AdminException("管理员不在线");
        if (Goods.goodsIdExist(goodsId)) {
            Goods goods = Goods.getGoods(goodsId);
            goods.goodsDown();
            return true;
        }
        throw new GoodsExistException("商品已下架");
    }

    /**
     * 管理员下线
     *
     * @return
     */
    public boolean adminUnLine(int adminId) {
        if (this.adminState != ADMIN_ONLINE) {
            throw new AdminException("管理员状态异常");
        }

        this.adminState = ADMIN_UNLINE;
        this.save();
        return true;
    }
}
